package com.cosmosource.core.advice;

import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import javax.validation.constraints.NotNull;
import java.io.Serializable;

/**
 * API 统一返回结果集对象
 *
 * @author Hsiao Lin
 */
@Data
public class ResponseEntity<T> implements Serializable {

    private final static String EMPTY = StringUtils.EMPTY;

    private Object code;

    private String info;

    private T data;

    public ResponseEntity() {
    }

    public ResponseEntity(@NotNull Object code) {
        this(code, EMPTY);
    }

    public ResponseEntity(@NotNull ResponseStatus responseStatus) {
        this(responseStatus.code(), responseStatus.info(), null);
    }

    public ResponseEntity(@NotNull Object code, String info) {
        this(code, info, null);
    }

    public ResponseEntity(@NotNull ResponseStatus responseStatus, T data) {
        this.code = responseStatus.code();
        this.info = responseStatus.info();
        this.data = data;
    }

    public ResponseEntity(@NotNull Object code, String info, T data) {
        this.code = code;
        this.info = info;
        this.data = data;
    }

    /**
     * 通用请求成功
     *
     * @return ResponseEntity<T>
     */
    public static <T> ResponseEntity<T> success() {
        return success(ResponseStatus.COSMO_00000.info(), null);
    }

    /**
     * 通用请求成功
     *
     * @param data 数据
     * @return ResponseEntity<T>
     */
    public static <T> ResponseEntity<T> success(T data) {
        return success("请求成功", data);
    }

    /**
     * 通用请求成功
     *
     * @param message 信息
     * @param data    数据
     * @return ResponseEntity<T>
     */
    public static <T> ResponseEntity<T> success(String message, T data) {
        return new ResponseEntity<>(ResponseStatus.COSMO_00000.code(), message, data);
    }

    /**
     * 通用请求失败 - 服务端异常
     *
     * @return ResponseEntity<T>
     */
    public static <T> ResponseEntity<T> error() {
        return error(ResponseStatus.COSMO_B0001.info());
    }

    /**
     * 通用请求失败 - 服务端异常
     *
     * @param message 错误信息
     * @return ResponseEntity<T>
     */
    public static <T> ResponseEntity<T> error(String message) {
        return error(message, null);
    }

    /**
     * 通用请求失败 - 服务端异常
     *
     * @param message 错误信息
     * @param data    错误数据
     * @return ResponseEntity<T>
     */
    public static <T> ResponseEntity<T> error(String message, T data) {
        return new ResponseEntity<>(ResponseStatus.COSMO_B0001.code(), message, data);
    }


    /**
     * 通用请求失败 - 指定code异常
     * <p>
     * 可以指定code来封装响应体
     *
     * @param responseStatus 响应状态 - 其中包含code和info
     * @param message        错误信息 - 具体的错误简述
     * @param data           错误数据 - 错误时供调用方使用的数据
     * @return ResponseEntity<T>
     */
    public static <T> ResponseEntity<T> error(ResponseStatus responseStatus, String message, T data) {
        return new ResponseEntity<>(responseStatus.code(), responseStatus.info() + (StringUtils.isEmpty(message) ?
                "" : ":" + message), data);
    }

    /**
     * 通用请求失败 - 请求失败
     *
     * @return ResponseEntity<T>
     */
    public static <T> ResponseEntity<T> failure() {
        return failure(ResponseStatus.COSMO_A0001.info());
    }

    /**
     * 通用请求失败 - 请求失败
     *
     * @param message 失败信息
     * @return ResponseEntity<T>
     */
    public static <T> ResponseEntity<T> failure(String message) {
        return failure(message, null);
    }

    /**
     * 通用请求失败 - 请求失败
     *
     * @param message 失败信息
     * @param data    失败数据
     * @return ResponseEntity<T>
     */
    public static <T> ResponseEntity<T> failure(String message, T data) {
        return new ResponseEntity<>(ResponseStatus.COSMO_A0001.code(), message, data);
    }

}
